package com.xwb.service.impl;

import com.xwb.dao.MenuDao;
import com.xwb.dao.UserDao;
import com.xwb.domain.LoginUser;
import com.xwb.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @Author 三更  B站： https://space.bilibili.com/663528522
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private MenuDao menuDao;

    @Override
    public UserDetails loadUserByUsername(String userId) throws UsernameNotFoundException {
        //根据用户名查询用户信息
        User user = userDao.selectStudentById(userId);
        //如果查询不到数据就通过抛出异常来给出提示
        if (Objects.isNull(user)) {
            throw new RuntimeException("用户Id或密码错误");
        }
        //TODO 根据用户查询权限信息 添加到LoginUser中
        List<String> list = menuDao.selectPermsByUserId(user.getUserId());
        //封装成UserDetails对象返回 
        return new LoginUser(user, list);
    }
}